package cn.com.org.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.BasicUpdate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.mongodb.util.JSON;

import cn.com.org.cinterface.IServiceBase;

public class ServiceBaseImpl implements IServiceBase {

	 @Autowired
	 private MongoTemplate mongoTemplate;
	 public ServiceBaseImpl(){
		 
	 }
	@Override
	public  <T> Boolean addObject(T obj) {
		Boolean result=false;
		mongoTemplate.save(obj);
		return true;
	}

	@Override
	public <T> Integer deleteObjectById(UUID id,Class<T> t) {
		 // WriteResult wr=mongoTemplate.remove(new Query(Criteria.where("id").is(id)),t );
		  Integer count= deleteObject("id",id,t);
		  return count;
	}
	@Override
	public <T> Integer deleteObject(String key,Object value,Class<T> t){
		  WriteResult wr=mongoTemplate.remove(new Query(Criteria.where(key).is(value)),t );
		  Integer count= wr.getN();
		  return count;
	}
	@Override
	public <T> Integer updateObjectById(UUID id, String json,Class<T> t,String... exclude) {
		BasicDBObject basicDbbase=new BasicDBObject().parse(json);
		for (String key : exclude) {
			basicDbbase.remove(key);
		}
		//DBObject dbObject = (DBObject) JSON.parse(json);
		DBObject dbobject=new BasicDBObject();
		dbobject.put("$set", basicDbbase);
		//Update update=new BasicUpdate(dbobject);
	
				
		   WriteResult wr=mongoTemplate.updateFirst(new Query(Criteria.where("id").is(id)),Update.fromDBObject(dbobject,exclude),t);
			//mongoTemplate.updateFirst(new Query(Criteria.where("id").is(id)),Update.fromDBObject(, ""), Employee.class);
		 // mongoTemplate.findAndModify(new Query(Criteria.where("id").is(id)),Update.fromDBObject(dbObject), t);
		   return wr.getN();	 
	}
	@Override
	public <T> Integer updateOneObject(String key,Object value,String updatekey,Object updatevalue,Class<T> t){
		WriteResult wr=mongoTemplate.updateFirst(new Query(Criteria.where(key).is(value)),Update.update(updatekey, updatevalue) ,t);
		return wr.getN();
	}
	@Override
	public <T> Integer updateObject(String key,Object value,String updatekey,Object updatevalue,Class<T> t){
		WriteResult wr=mongoTemplate.updateMulti(new Query(Criteria.where(key).is(value)),Update.update(updatekey, updatevalue) ,t);
		return wr.getN();
	}
	@Override
	public <T> Integer updateObject(String conjson,String updateresult,Class<T> t,String... exclude){
		BasicDBObject basicDbbase=new BasicDBObject().parse(updateresult);
		for (String key : exclude) {
			basicDbbase.remove(key);
		}
		//DBObject conditionQuery = (DBObject)JSON.parse(conjson);
		DBObject dbobject=new BasicDBObject();
		dbobject.put("$set", basicDbbase);
		WriteResult wr=mongoTemplate.updateMulti(new BasicQuery(conjson),Update.fromDBObject(dbobject, exclude) ,t);
		return wr.getN();
	}
	
	@Override
	public  <T> T getObject(String key, Object value,Class<T> t) {
		T result=mongoTemplate.findOne(new Query(Criteria.where(key).is(value)),t);
		return result;		
	}
	@Override
	public <T> boolean isExists(String key,Object value,String key2,Object value2,Class<T> t){
	    Boolean result=mongoTemplate.exists(new Query(Criteria.where(key).is(value).and(key2).is(value2)),t);
		return result;
	}
	@Override
	public <T> T getById(Object id,Class<T> t){
	    
		   T result=	mongoTemplate.findById(id, t);
			return result;
		}
	@Override
	public  <T> List<T> getObjectList(String key, Object value,Class<T> t) {
		List<T> result=mongoTemplate.find(new Query(Criteria.where(key).is(value)),t);
		return result;
	}

	@Override
	public  <T> List<T> getObjectList(String jsonquery, Integer currentpage,
			Integer skip,Class<T> t) {
		
		List<T>  resultlist=mongoTemplate.find(new BasicQuery(jsonquery),t);
		return resultlist;
	}
	@Override
	public <T>  List<T> getObjectList(String jsonquery, Integer currentpage,Integer limit,
			Boolean orderdirection, Class<T> t,String... orderpfield) {
		Integer skip=(currentpage-1)*limit;
		
		//Query query=new BasicQuery(jsonquery).with(new Sort(new Order(Direction.DESC,""))).skip(skip).limit(limit);
		
		Query query=new BasicQuery(jsonquery).with(new Sort(Sort.Direction.DESC,orderpfield)).skip(skip).limit(limit);
		//Query query=new BasicQuery(jsonquery).with(new Sort(Sort.Order(Direction.ASC,orderpfield))).skip(skip).limit(limit);
		
		List<T>  resultlist=mongoTemplate.find(new BasicQuery(jsonquery),t);
		return resultlist;
	}
	@Override
	public <T> List<T> getAll(Class<T> t) {
       List<T>	 alldata=mongoTemplate.findAll(t);
		return alldata;
	}
}
